package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Issue;
import com.xc.freeapp.entity.Relation;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.request.bean.RelationAndIssue;
import com.xc.freeapp.service.RelationService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.TokenAnnotation;
import com.xc.freeapp.util.UploadUtil;

/**
 * 家庭病例模块controller
 * 
 * @author ZhouXinjie
 */

@Controller
@RequestMapping("/relation")
@Api(value="/relation",description = "家庭病例")
public class RelationController extends AbstractController {
	@Autowired
	private RelationService relationService;

	/**
	 * 根据关系人ID查询关系
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据ID查询关系")
	public Relation selectRelationById(@PathVariable("id") Integer id)
			throws BaseException {
		
		Relation relation = relationService.selectByPrimaryKey(id);
		relation.setHeadpic(UploadUtil.urlConversion(relation.getHeadpic()));
		return relation;
	}
	/**
	 * 根据组标识获取关系列表
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/byGroupType", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "根据组标识获取关系列表")
	public List<Relation> getRelationListByGroupType(@RequestParam("groupType") String groupType)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
	
		CheckUtils.notBlankByName(Constant.VER_NOTNULL_BYNAME, groupType,
				"groupType");
		log.info("根据分组标识" + groupType + "获取列表");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("groupType", groupType);
		map.put("userid", authInfo.getUserId());
		PageBounds pageBounds = new PageBounds(1, 100);
		List<Relation> list = relationService.getList(map,pageBounds);
		for (Relation relation : list) {
			relation.setHeadpic(UploadUtil.urlConversion(relation.getHeadpic()));
		}
		return list;
	}


	/**
	 * 修改关系
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "修改关系")
	public  Relation saveRelation(@RequestBody Relation relation)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		if(relation.getHeadpic()!=null&&!"".equals(relation.getHeadpic())){
			relation.setHeadpic(UploadUtil.upload(relation.getHeadpic(),authInfo.getAppIntId()));
		}
		relation.setUpdateby(Integer.parseInt(authInfo.getUserId()));
		relationService.updateByPrimaryKey(relation);
		Relation retrelation = relationService.selectByPrimaryKey(relation
				.getId());
		retrelation.setHeadpic(UploadUtil.urlConversion(retrelation.getHeadpic()));
		return retrelation;
	}

	/**
	 * 保存关系以及就诊记录
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "/relationAndIssue", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存关系以及就诊记录")
	public Map<String,Object> saveRelationAndIssue(
			@RequestBody RelationAndIssue relationAndIssue) throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		Relation relation = relationAndIssue.getRelation();
		relation.setHeadpic(UploadUtil.upload(relation.getHeadpic(),authInfo.getAppIntId()));

		Issue issue = relationAndIssue.getIssue();

		issue.setPics(UploadUtil.uploads(issue.getPiclist(),authInfo.getAppIntId()));
		return relationService.saveRelationAndIssue(relationAndIssue,authInfo.getUserId());
	}

	/**
	 * 删除关系
	 * */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = " 删除关系")
	public ResponseBean deleteRelation(@RequestParam("id") String id) throws BaseException {
		log.info("删除关系记录");
		int del = relationService.deleteByPrimaryKey(Integer.parseInt(id));
		if (del > 0) {
			return ResponseBean.getSuccess();
		} else {
			throw new BaseException(Constant.COMMON_ERROR);
		}
	}
}
